package com.ykht.proj.coupon.controller.b;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.LookRoleTypeEnum;
import com.ykht.proj.coupon.entity.CnBrand;
import com.ykht.proj.coupon.entity.FxConfRewardBase;
import com.ykht.proj.coupon.service.CnUserExtService;
import com.ykht.proj.coupon.service.FxConfRewardBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author: lazyBoy
 * @Date: 2021/2/24 19:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/b/fxconfrewardbase")
@Api(value = "/b/fxconfrewardbase", tags = "品牌佣金配置管理")
public class BxConfRewardBaseController {
	private final FxConfRewardBaseService fxConfRewardBaseService;
	private final CnUserExtService cnUserExtService;

	/**
	 * 展示品牌佣金配置
	 *
	 * @return R
	 */
	@ApiOperation(value = "展示品牌佣金配置", notes = "展示品牌佣金配置")
	@SysLog("展示品牌佣金配置")
	@GetMapping
	public R show() {
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		return R.ok(fxConfRewardBaseService.find(cnBrand));
	}

	/**
	 * 新增或修改品牌佣金配置
	 *
	 * @param fxConfRewardBase 品牌佣金配置
	 * @return R
	 */
	@ApiOperation(value = "新增或修改品牌佣金配置", notes = "新增或修改品牌佣金配置")
	@SysLog("修改品牌佣金配置")
	@PostMapping
	public R updateById(@RequestBody FxConfRewardBase fxConfRewardBase) {
		CnBrand cnBrand = new CnBrand().selectById(SecurityUtils.getSjId());
		FxConfRewardBase result = fxConfRewardBaseService.find(cnBrand);
		if (result != null) {
			fxConfRewardBase.setId(result.getId());
			fxConfRewardBase.setUpdateUser(SecurityUtils.getUserId());
			fxConfRewardBase.setUpdateTime(LocalDateTime.now());
			return R.ok(fxConfRewardBaseService.updateById(fxConfRewardBase));
		} else {
			fxConfRewardBase.setBrandId(cnBrand.getId());
			fxConfRewardBase.setCreateUser(SecurityUtils.getUserId());
			fxConfRewardBase.setCreateTime(LocalDateTime.now());
			return R.ok(fxConfRewardBaseService.save(fxConfRewardBase));
		}
	}
}
